cc.Class({
    extends: cc.Component,

    properties: {
    },

    // use this for initialization
    onLoad: function () {
        this.materialize();
    },

    init: function (game, id, ownerId, arenaId, x, y, alpha) {
        this.game = game;
        this.id = id;
        this.ownerId = ownerId;
        this.arenaId = arenaId;
        this.x = x;
        this.y = y;
        this.alpha = alpha;
    },

    materialize: function () {
        this.node.x = this.x;
        this.node.y = this.y;
        var anim = this.getComponent(cc.Animation);
        anim.play("normal_ball");
        this.node.rotation = this.alpha * 180 / Math.PI;
    },

    explode: function () {
        var anim = this.getComponent(cc.Animation);
        anim.play("explode_ball");
        anim.on('finished', this.onFinished, this);
    },

    onFinished: function () {
        // this.node.removeFromParent();
        this.node.destroy();
    },

    onDestroy: function () {
        delete this.game.balls[this.id];
    },
    // called every frame, uncomment this function to activate update callback
    update: function (dt) {
        if (this.node.x < (- cc.Config.WIDTH ) ||
            this.node.x > cc.Config.WIDTH  ||
            this.node.y < (- cc.Config.HEIGHT ) ||
            this.node.y > cc.Config.HEIGHT) {
            this.onFinished();
        } else {
            this.fly(dt);
        }
    },

    fly: function (dt) {
        if(this.exploding){
            return;
        }
        //move to trayectory
        var speedX = dt * cc.Config.BALL_SPEED * Math.sin(this.alpha);
        var speedY = dt * cc.Config.BALL_SPEED * Math.cos(this.alpha);
        this.node.x += speedX;
        this.node.y += speedY;
    }
});
